package camel.first.api.javaProcessor;

import camel.first.api.service.IXMLParserService;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.osgi.extensions.annotation.ServiceReference;

/**
 * User: yuxinlei
 * Date: 4/8/11
 * Time: 2:03 AM
 */
public class JAVAProcessor  extends RouteBuilder implements InitializingBean, DisposableBean {
    private static final Logger logger = LoggerFactory.getLogger(JAVAProcessor.class)  ;

    private String queueName = "xmlFileQueue";
    private String persistanceQueneName = "store";


    private IXMLParserService xmlParserService;



    @Override
    public void configure() {
        from("activemq:queue:" + queueName).split(body(String.class).tokenize("\n"))
        .process( new Processor(){
            public void process(Exchange exchange) {
                String line = (String)exchange.getIn().getBody();
                logger.debug("the xml content is " + line);
                 xmlParserService.transFileContent(line,"name");
                xmlParserService.transFileContent("aaaa","name")  ;
            }
        }

        ).to("activemq:queue:" + persistanceQueneName);
    }

    public void destroy() {
        logger.debug("Shutting down the processor route.");
    }

    public void afterPropertiesSet() {
        logger.debug("Initializing the processor route!");
		if (xmlParserService == null) {
			throw new BeanInitializationException("You must set the xmlParserService!");
		}
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    //@ServiceReference
    public void setXmlParserService(IXMLParserService xmlParserService) {
        this.xmlParserService = xmlParserService;
    }

    public String getPersistanceQueneName() {
        return persistanceQueneName;
    }

    public void setPersistanceQueneName(String persistanceQueneName) {
        this.persistanceQueneName = persistanceQueneName;
    }
}
